package cn.fl.warrant.controller;

import static org.junit.Assert.assertNotNull;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysUlFilesRecService;
import cn.fl.system.vo.SysUlFilesRecVO;
import cn.fl.warrant.service.IArcApplyDetailService;
import cn.fl.warrant.service.IArcApplyMainService;
import cn.fl.warrant.service.IArcMgrDetailService;
import cn.fl.warrant.service.IArcMgrMainService;
import cn.fl.warrant.service.IArcPostInfoService;
import cn.fl.warrant.service.IArcTransferDetailService;
import cn.fl.warrant.transform.ArcMgrDetailVOTransform;
import cn.fl.warrant.transform.ArcMgrMainVOTransform;
import cn.fl.warrant.vo.ArcApplyDetailVO;
import cn.fl.warrant.vo.ArcApplyMainVO;
import cn.fl.warrant.vo.ArcMgrDetailVO;
import cn.fl.warrant.vo.ArcMgrMainVO;
import cn.fl.warrant.vo.ArcPostInfoVO;
import cn.fl.warrant.vo.ArcSysFlowSHVO;
import cn.fl.warrant.vo.ArcTransferDetailVO;
import fl.warrant.facade.archive.api.IArcApplyFlowBusnessApiService;
import fl.warrant.facade.archive.api.IArcApplyMainApiService;
import fl.warrant.facade.archive.api.IArcMgrDetailApiService;
import fl.warrant.facade.archive.api.IArcMgrMainApiService;
import fl.warrant.facade.archive.condition.ArcApplyDetailQC;
import fl.warrant.facade.archive.condition.ArcApplyMainQC;
import fl.warrant.facade.archive.condition.ArcMgrDetailQC;
import fl.warrant.facade.archive.condition.ArcMgrMainQC;
import fl.warrant.facade.archive.condition.ArcPostInfoQC;
import fl.warrant.facade.archive.constant.ArchiveConstants;
import fl.warrant.facade.archive.dto.ArcMgrMainDTO;
import net.sf.json.JSONArray;

/**
 * @Description: 归档申请主表(旧表：T_API_DOCUMENT_DOSSIERAPPLY) 控制层
 */
@Controller
@RequestMapping("/archive/arcApplyMain")
public class ArcApplyMainController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(ArcApplyMainController.class);

	/**
	 * ArcApplyMain 服务层BEAN
	 */
	@Autowired
	private IArcApplyMainService arcApplyMainService;
	@Autowired
	private IArcApplyDetailService arcApplyDetailService;
	@Autowired
	private IArcMgrMainService arcMgrMainService;
	@Autowired
	private IArcMgrDetailService arcMgrDetailService;
	@Autowired
	private IArcPostInfoService arcPostInfoService;
	@Autowired
	private IArcApplyFlowBusnessApiService arcApplyFlowBusnessApiService;
	@Autowired
	private ISysUlFilesRecService sysUlFilesRecService;
	@Autowired
	private IArcTransferDetailService arcTransferDetailService; 
	@Autowired
	private IArcApplyMainApiService arcApplyMainApiService;
	@Autowired
	private IArcMgrMainApiService arcMgrMainApiService;
	@Autowired
	private IArcMgrDetailApiService arcMgrDetailApiService;
	
	private Map getParms(HttpServletRequest request){
		Map map=new HashMap();
		Enumeration enu=request.getParameterNames();  
		StringBuffer sb = new StringBuffer();
		while(enu.hasMoreElements()){  
		String paraName=(String)enu.nextElement();  
			map.put(paraName, request.getParameter(paraName));
		}
		return map;
	}
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("archive/arcApplyMain/list");
		BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("archive/arcApplyMain/modify");
		view.addObject("id", id);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(ArcApplyMainQC qc,HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String searchName = request.getParameter("demo.name");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		
		if (StringUtils.isNotEmpty(searchName)) {
			//TODO SOMTHING
		}
		page.setCondition(qc);

		//TODO SOMETHING
		Pager pager = new Pager();//this.arcApplyMainService.searchListPage(page);
		List<ArcApplyMainVO> list = (List<ArcApplyMainVO>) pager.getResultList();
		ListPageVO<ArcApplyMainVO> listPage = new ListPageVO<ArcApplyMainVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.arcApplyMainService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			ArcApplyMainVO vo = this.arcApplyMainService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		ArcApplyMainVO entity = null;
		try {
			entity = new ArcApplyMainVO();
			//TODO ADD PARAM
			Long id = this.arcApplyMainService.add(entity);
			result.setData(id);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		ArcApplyMainVO entity = null;
		try {
			entity = new ArcApplyMainVO();
			entity.setId(Long.valueOf(id));
			//entity.setName(name);
			//TODO ADD PARAM
			boolean success = this.arcApplyMainService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	/**
	 * 到流程列表页面
	 */
	@RequestMapping(value = "/getFlow.do", method = { RequestMethod.GET})
	public String getFlow(HttpServletRequest request, HttpServletResponse response) {
		String flag = request.getParameter("flag");
		try {
			
			String id = request.getParameter("aid");
			
			request.setAttribute("flag",request.getParameter("flag"));
			//查找申请单
			ArcApplyMainVO avo= this.arcApplyMainService.selectById(Long.valueOf(id));
			//avo.setArcApplyDetailList(aList);
			String contractNo=avo.getContractNo();
			request.setAttribute("avo",avo);
			//查找筛选条件
			ArcMgrMainQC qc=new ArcMgrMainQC();
	//		if (StringUtils.isNotBlank(paySchId)) {
	//			qc.setPaySchId(Long.parseLong(paySchId));
	//		}
			qc.setContractNo(contractNo);
			Pager pager=new Pager();
			pager.setCurrentPage(0);
			pager.setEveryPage(10000);
			pager.setCondition(qc);
			List<ArcMgrMainVO> mgrList=(List<ArcMgrMainVO>)this.arcMgrMainService.selectListForListPage(pager).getResultList();			
			ArcMgrMainVO mgr=mgrList.get(0);
//			if (avo.getPortfolioNumber()!=null) {
//				mgr.setPortfolioNumber(avo.getPortfolioNumber());
//			}
//			if (avo.getPortfolioNumberProvince()!=null) {
//				mgr.setPortfolioNumberProvince(avo.getPortfolioNumberProvince());
//			}
			request.setAttribute("mgr",mgr);//筛选条件	
			
			//申请单明细
			ArcApplyDetailQC dQc=new ArcApplyDetailQC();
			dQc.setArcApplyMainId(Long.valueOf(id));
			List<ArcApplyDetailVO> aList=this.arcApplyDetailService.searchList(dQc);
			
			List<ArcApplyDetailVO> cList=new ArrayList<ArcApplyDetailVO>();//合同明细
			for (int i = 0; i < aList.size(); i++) {
				ArcApplyDetailVO vo=aList.get(i);
				if (vo.getContractNo()!=null&&vo.getPaylistCode()==null) {
					cList.add(vo);
				}
			}
			List<Map> pList=new ArrayList<Map>();//支付表明细
			
			for (int i = 0; i < mgrList.size(); i++) {
				ArcMgrMainVO vo=mgrList.get(i);
				Map map=new HashMap();
				map.put("paySchNo", vo.getPaySchNo());
				map.put("checkbz", "抵押登记证书真伪识别结果：无须上传");
				String checkbz="";
				List<ArcApplyDetailVO> ppList=new ArrayList<ArcApplyDetailVO>();//支付表明细
				for (int j = 0; j < aList.size(); j++) {
					ArcApplyDetailVO vo2=aList.get(j);
					if (vo2.getContractNo()!=null&&vo2.getPaylistCode()!=null&&vo.getPaySchNo().equals(vo2.getPaylistCode())) {
						ppList.add(vo2);
						
						//登记证书抵押页校验结果
						if (vo2.getFileName().contains(ArchiveConstants.VE_NAME)
								) {
							if (vo2.getBz()!=null) {
								checkbz +="抵押1结果："+vo2.getBz()+"。";
							}
							
							
						}
						if (vo2.getFileName().contains(ArchiveConstants.VE_NAME2)
								) {
							if (vo2.getBz()==null) {
								checkbz +="抵押2结果：没有附件。";
							}else {
								checkbz +="抵押2结果："+vo2.getBz()+"。";
							}
							
									
						}
					}
				}
				map.put("checkbz", checkbz);
				map.put("ppList", ppList);
				//添加租赁物信息
				//map.put("dtl", vo.getDtl());
				map.put("mvo", vo);
				if (ppList.size()>0) {
					pList.add(map);
				}
			}
			request.setAttribute("id",id);
			request.setAttribute("cList",cList);
			request.setAttribute("pList",pList);
			request.setAttribute("cjson",JSON.toJSON(cList));
			request.setAttribute("pjson",JSON.toJSON(pList));
			this.arcMgrMainService.setAttribute(request);//筛选条件查询
			//邮寄信息
			List<ArcPostInfoVO> plist=new ArrayList<ArcPostInfoVO>();
					
					ArcPostInfoQC condition2=new ArcPostInfoQC();
					condition2.setArcAppId(Long.valueOf(id));
					plist=this.arcPostInfoService.searchList(condition2);
			//初始化
			if (plist!=null) {
				for (int i = 0; i < plist.size(); i++) {
					ArcPostInfoVO post=plist.get(i);
					//客户经理
					if ("003".equals(flag)&& "1".equals(post.getSendPorsonType())) {
						request.setAttribute("post",post);
						plist.remove(i);
						break;
					}
					//商助
					if ("001".equals(flag)&& "2".equals(post.getSendPorsonType())) {
						request.setAttribute("post",post);
						plist.remove(i);
						break;
					}
				}
			}
			
			request.setAttribute("plist",plist);
			BaseLogUtils.info(logger, "getFlow", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());

		} catch (Exception e) {
			// TODO: handle exception
			BaseLogUtils.error(logger, "getFlow", e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		if ("003".equals(flag)) {//客户经理重新提交
			return "archive/arcMgr/arcApplyFlowC";
		}else {//其他页面
			return "archive/arcMgr/arcApplyFlow";
		}
		
	}
	/**
	 * 修改
	 */
	@RequestMapping(value = "/modifyFlow.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modifyFlow(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String flag = request.getParameter("flag");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modifyFlow", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		ArcApplyMainVO entity = null;
		try {
			entity = this.arcApplyMainService.selectById(Long.valueOf(id));
			//档案袋编号放入档案记录表
			ArcMgrMainQC mgrMainQC=new ArcMgrMainQC();
			mgrMainQC.setContractNo(entity.getContractNo());
			List<ArcMgrMainVO> mgrlist=this.arcMgrMainService.selectList(mgrMainQC);
			ArcMgrMainVO mgrMainVO=mgrlist.get(0);
			//档案袋编号生成
			String code="";
			if ("001".equals(flag) && StringUtils.isBlank(mgrMainVO.getPortfolioNumberProvince())) {//省
				code=this.arcApplyMainService.createPortfolioNumber(entity, "1");
				for (ArcMgrMainVO vo:mgrlist) {
					vo.setPortfolioNumberProvince(code);
					this.arcMgrMainService.modify(vo);
				}
			}else if("002".equals(flag) && StringUtils.isBlank(mgrMainVO.getPortfolioNumber())) {//总部
				code=this.arcApplyMainService.createPortfolioNumber(entity, "2");
				for (ArcMgrMainVO vo:mgrlist) {
					vo.setPortfolioNumber(code);
					this.arcMgrMainService.modify(vo);
				}
			}
			
			entity.setMdfUsrId(new Long(flag));
			this.arcApplyMainService.modify(entity);
			
			
			
			//申请单明细
			ArcApplyDetailQC dQc=new ArcApplyDetailQC();
			dQc.setArcApplyMainId(Long.valueOf(id));
			List<ArcApplyDetailVO> cList=this.arcApplyDetailService.searchList(dQc);
			Map map=this.getParms(request);
			
			//entity.setName(name);
			if (cList==null) {
				cList=new ArrayList<ArcApplyDetailVO>();
			}
			//更新合同明细 支付表明细
			for (int i = 0; i < cList.size(); i++) {
				ArcApplyDetailVO vo=cList.get(i);
				//实际归档份数
				if (map.get("checkCount_p_"+vo.getId())!=null&&!"".equals(map.get("checkCount_p_"+vo.getId()))) {
					vo.setCheckCount(Long.valueOf(map.get("checkCount_p_"+vo.getId()).toString().replaceAll(" ", "")));
				}else {
					//vo.setCheckCount(0L);
				}
				//要求归档份数
				if (map.get("checkCount_d_"+vo.getId())!=null&&!"".equals(map.get("checkCount_d_"+vo.getId()))) {
					vo.setDossierCount(Long.valueOf(map.get("checkCount_d_"+vo.getId()).toString().replaceAll(" ", "")));
				}
				this.arcApplyDetailService.modify(vo);
			}
			//保存邮寄信息
			ArcPostInfoVO pVo=new ArcPostInfoVO();
			pVo.setArcAppId(Long.valueOf(id));
			//保存邮寄信息
			map.put("arcAppId", id);
			map.put("arcAppType", "1");
			map.put("arcAppBussType", "0");
			//商助
			
			if ("001".equals(flag)) {
				map.put("sendPorsonType", "2");
				this.arcPostInfoService.addOrUpdate(map);
			}
			
//			this.arcApplyFlowBusnessApiService.updateMgrByApply(Long.valueOf(id), true);
//			boolean success = this.arcApplyMainService.modify(entity);
//			if(!success){
//				result.setSuccess(BaseConstant.IS_YESNO_NO);
//				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
//				BaseLogUtils.info(logger, "modifyFlow", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
//			}
			BaseLogUtils.info(logger, "modifyFlow", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modifyFlow", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modifyFlow", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	/**
	 * 修改客户经理重新提交
	 */
	@RequestMapping(value = "/modifyFlowC.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modifyFlowC(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String flag = request.getParameter("flag");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modifyFlow", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		ArcApplyMainVO entity = null;
		try {
			entity = this.arcApplyMainService.selectById(Long.valueOf(id));
			entity.setMdfUsrId(new Long(flag));
			this.arcApplyMainService.modify(entity);
			
			//更新电子合同
			String contractNo=entity.getContractNo();
			ArcMgrMainVO cVo=null;
			ArcMgrMainQC qc=new ArcMgrMainQC();
			qc.setContractNo(contractNo);
			String sysCd = CurrentThreadContext.getCurrentSysCd();
			String orgCd = CurrentThreadContext.getCurrentOrgCd();
			//已经归档的支付表
			List<ArcMgrDetailVO> ddlist=new ArrayList<ArcMgrDetailVO>();
			List<ArcMgrMainVO> mgrList=(List<ArcMgrMainVO>)this.arcMgrMainService.selectList(qc);
			for (int i = 0; i < mgrList.size(); i++) {
				ArcMgrMainVO arcMgrMainVO =mgrList.get(i);
				ArcMgrDetailQC dQc = new ArcMgrDetailQC();
				dQc.setArcMgrMainId(arcMgrMainVO.getId());
				List<ArcMgrDetailVO> list = ArcMgrDetailVOTransform
						.toVOList(this.arcMgrDetailApiService.searchList(dQc, sysCd, orgCd));
				arcMgrMainVO.setArcMgrDetailList(list);
				if (arcMgrMainVO.getPaySchId()==null) {
					cVo=arcMgrMainVO;
				}
				ddlist.addAll(list);
			}			 
			this.arcMgrMainService.setFilSign(mgrList, cVo);			
			
			//申请单明细
			ArcApplyDetailQC dQc=new ArcApplyDetailQC();
			dQc.setArcApplyMainId(Long.valueOf(id));
			List<ArcApplyDetailVO> cList=this.arcApplyDetailService.searchList(dQc);
			Map map=this.getParms(request);
			
			//entity.setName(name);
			if (cList==null) {
				cList=new ArrayList<ArcApplyDetailVO>();
			}
			
			//更新合同明细 支付表明细
			for (int i = 0; i < cList.size(); i++) {
				ArcApplyDetailVO vo=cList.get(i);
				if (map.get("thisCount_p_"+vo.getId())!=null&&!"".equals(map.get("thisCount_p_"+vo.getId()))) {
					vo.setThisCount(Long.valueOf(map.get("thisCount_p_"+vo.getId()).toString().replaceAll(" ", "")));
				}else {
					vo.setThisCount(0L);
				}
				//有外联附件的更新eleid
				if (map.get("ele_"+vo.getId())!=null&&!"".equals(map.get("ele_"+vo.getId()))) {
					vo.setEleId(Long.valueOf(map.get("ele_"+vo.getId()).toString().replaceAll(" ", "")));
				}
				//更新电子合同
				if (vo.getDossierTemp().equals("3")) {
					for (ArcMgrDetailVO dvo: ddlist) {
											
						if (dvo.getArcReceiptDetailId().equals(vo.getArcReceiptDetailId())
								&&(dvo.getPaylistCode()==null||dvo.getPaylistCode().equals(vo.getPaylistCode())) 
							&&dvo.getEleId()!=null) {
							vo.setEleId(dvo.getEleId());
							vo.setCheckCount(vo.getDossierCount());
						}
					}
				}	
				this.arcApplyDetailService.modify(vo);
			}
			//保存邮寄信息
			ArcPostInfoVO pVo=new ArcPostInfoVO();
			pVo.setArcAppId(Long.valueOf(id));
			//保存邮寄信息
			map.put("arcAppId", id);
			map.put("arcAppType", "1");
			map.put("arcAppBussType", "0");
			//客户经理
				map.put("sendPorsonType", "1");
				this.arcPostInfoService.addOrUpdate(map);
			
//			this.arcApplyFlowBusnessApiService.updateMgrByApply(Long.valueOf(id), true);
//			boolean success = this.arcApplyMainService.modify(entity);
//			if(!success){
//				result.setSuccess(BaseConstant.IS_YESNO_NO);
//				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
//				BaseLogUtils.info(logger, "modifyFlow", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
//			}
			BaseLogUtils.info(logger, "modifyFlow", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modifyFlow", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modifyFlow", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	/**
	 * 到流程页面
	 */
	@RequestMapping(value = "/toViewFlow.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toViewFlow(HttpServletRequest request, HttpServletResponse response) {
		String contractNo = request.getParameter("contractNo");
		ModelAndView view = new ModelAndView("archive/arcMgr/arcApplyFlowList");
		view.addObject("contractNo", contractNo);
		BaseLogUtils.info(logger, "toViewFlow", "跳转到页面.[contractNo="+contractNo+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 流程数据
	 * @param request
	 * @param response
	 * @return
	 * 2018年3月15日 
	 * lxb
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initFlowList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initFlowList(ArcApplyMainQC qc,HttpServletRequest request, HttpServletResponse response) {
//		String currentPage = request.getParameter("page");
//		String pageSize = request.getParameter("pagesize");
//		String searchName = request.getParameter("demo.name");
		String contractNo = request.getParameter("contractNo");
		if (StringUtils.isBlank(contractNo)) {
			return null;
		}
		//ArcApplyMainQC qc=new ArcApplyMainQC();
		qc.setContractNo(contractNo);
		List<ArcSysFlowSHVO> list=this.arcApplyMainService.searchApplyFlow(qc);
		ListPageVO<ArcSysFlowSHVO> listPage = new ListPageVO<ArcSysFlowSHVO>(list, list.size());
		BaseLogUtils.info(logger, "initFlowList", "查询数据完成. DATA:" + JSONObject.toJSONString(list), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
	/**
	 * 到上传文件页面
	 */
	@RequestMapping(value = "/toUpload.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toUpload(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");//
		ModelAndView view = new ModelAndView("archive/arcMgr/axcam");
		view.addObject("id", id);
		view.addObject("flag", request.getParameter("flag"));
		BaseLogUtils.info(logger, "toUpload", "跳转到页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 上传文件保存
	 * @param request
	 * @param response
	 * @return
	 * 2018年3月20日 
	 * lxb
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveUpload.do", method = { RequestMethod.POST})
	@ResponseBody
	public Object saveUpload(HttpServletRequest request, HttpServletResponse response) {
		String fileParams = request.getParameter("fileParams");//文件资料
		String id = request.getParameter("id");//
		String flag = request.getParameter("flag");//
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(fileParams)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "saveUpload", "修改数据,主键不存在.[fileParams="+fileParams+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		try {
			
			if(StringUtils.isNotBlank(fileParams)){
				List<SysUlFilesRecVO> fileList = new ArrayList<SysUlFilesRecVO>();
				if (StringUtils.isNotBlank(fileParams)) {
					fileList = JSON.parseArray(fileParams, SysUlFilesRecVO.class);
				}
				if (fileList.size()>0) {
					Long did=null;
					//查找档案id
					if (flag.equals("1")) {
						ArcApplyDetailVO d=this.arcApplyDetailService.selectById(new Long(id));
						ArcMgrDetailQC qc=new ArcMgrDetailQC();
						qc.setContractNo(d.getContractNo());
						qc.setPaylistCode(d.getPaylistCode());
						qc.setArcReceiptDetailId(d.getArcReceiptDetailId());
						List<ArcMgrDetailVO> voList=this.arcMgrDetailService.searchList(qc);
						
						if (voList!=null&&voList.size()>0) {
							did=voList.get(0).getId();
						}else {
							result.setSuccess(BaseConstant.IS_YESNO_NO);
							result.setInfo("档案不存在");
							BaseLogUtils.info(logger, "saveUpload", "失败.[id="+id+"] 档案不存在" , CurrentThreadContext.getCurrentUserName());
							return result;
						}
						result.setId(did);
						d.setEleId(did);
						//上传时间
						d.setUpUsrTm(new Date());
//						//校验抵押页
//						if (d.getFileName().contains(ArchiveConstants.VE_NAME)) {
//							String rootPath = PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_ROOT_PATH);
//							String image2path=rootPath+fileList.get(0).getFilePath();
//							DataResultDTO rdto=checkVehicleRegistrationApiService.checkVehicleRegistration(image2path, d.getId(), CurrentThreadContext.getCurrentUserId());
//							BaseLogUtils.info(logger, "saveUpload", "抵押页校验---"+rdto);
//							if (rdto.getCode().equals("00000")) {
//								d.setCheckCount(1L);
//							}else {
//								d.setCheckCount(0L);
//							}
//						}
						this.arcApplyDetailService.modify(d);
					}else if(flag.equals("2")){//移交
						ArcTransferDetailVO vo=arcTransferDetailService.selectById(new Long(id));
						did=vo.getId();
						result.setId(did);
						vo.setEleId(did);
						this.arcTransferDetailService.modify(vo);
					}
					
					for (SysUlFilesRecVO sysUlFilesRecVO : fileList) {
						sysUlFilesRecVO.setPkId(String.valueOf(did));
						sysUlFilesRecVO.setTabNm(ArchiveConstants.FILE_ARC_MGR_DETAIL);
						sysUlFilesRecVO.setIsDel(BaseConstant.IS_YESNO_NO);
						sysUlFilesRecVO.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
						sysUlFilesRecVO.setSysCd(CurrentThreadContext.getCurrentSysCd());
						sysUlFilesRecVO.setCfgCd(ArchiveConstants.ARC005);
						sysUlFilesRecVO.setCfgDtlCd(ArchiveConstants.DOSSOER_TEMP_3);
						sysUlFilesRecVO.setCrtTm(new Date());
						sysUlFilesRecVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
						/*SysUlFilesRecDTO dto = SysUlFilesRecVOTransform.toDTO(sysUlFilesRecVO);
						dtoFileList.add(dto);*/
						Long vid=this.sysUlFilesRecService.add(sysUlFilesRecVO);
					}
					
				}else {
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
				}
				
			}
		
		} catch (Exception e) {
			// TODO: handle exception
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(e.getMessage());
			BaseLogUtils.info(logger, "saveUpload", "失败.[id="+id+"] DATA:" +  e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveUploadPhoto.do", method = { RequestMethod.POST})
	@ResponseBody
	public Object saveUploadPhoto(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		StringBuilder sb = new StringBuilder();
		try(BufferedReader reader = request.getReader();) {
			char[] buff = new char[1024];
			int len;
			while((len = reader.read(buff)) != -1) {
				sb.append(buff,0, len);
		    }
		}catch (IOException e) {
		     e.printStackTrace();
		}
		Map jobject = JSON.parseObject(sb.toString(),Map.class);
		String datas =(String) jobject.get("datas");//文件资料
		String fileNames = (String) jobject.get("fileNames");//文件资料
//		String datas = request.getParameter("datas");//文件资料
//		String fileNames = request.getParameter("fileNames");//文件资料
		String id = request.getParameter("id");//
		String flag = request.getParameter("flag");//
		String filePath = request.getParameter("filePath");//
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(datas)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "saveUpload", "修改数据,主键不存在.[fileParams="+fileNames+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		try {
			
			if(StringUtils.isNotBlank(datas)){
				//上传文件
				List<String> datasList= JSON.parseArray(datas,String.class);
				List<String> fileNamesList= JSON.parseArray(fileNames,String.class);
				List<Map> list=new ArrayList<Map>();
				for (int i = 0; i < datasList.size(); i++) {
					Map map=new HashMap();
					map.put("data", datasList.get(i));
					map.put("fileName", fileNamesList.get(i));
					list.add(map);
				}				
				if (list!=null&&list.size()>0) {
					JSONArray params= sysUlFilesRecService.fileUploadB64(filePath, list);
					//保存文件
					List<SysUlFilesRecVO> fileList = JSON.parseArray(params.toString(), SysUlFilesRecVO.class);;
					
					if (fileList.size()>0) {
						Long did=null;
						//查找档案id
						if (flag.equals("1")) {
							ArcApplyDetailVO d=this.arcApplyDetailService.selectById(new Long(id));
							ArcMgrDetailQC qc=new ArcMgrDetailQC();
							qc.setContractNo(d.getContractNo());
							qc.setPaylistCode(d.getPaylistCode());
							qc.setArcReceiptDetailId(d.getArcReceiptDetailId());
							List<ArcMgrDetailVO> voList=this.arcMgrDetailService.searchList(qc);
							
							if (voList!=null&&voList.size()>0) {
								did=voList.get(0).getId();
							}else {
								result.setSuccess(BaseConstant.IS_YESNO_NO);
								result.setInfo("档案不存在");
								BaseLogUtils.info(logger, "saveUpload", "失败.[id="+id+"] 档案不存在" , CurrentThreadContext.getCurrentUserName());
								return result;
							}
							result.setId(did);
							d.setEleId(did);
							//上传时间
							d.setUpUsrTm(new Date());
//							//校验抵押页
//							if (d.getFileName().contains(ArchiveConstants.VE_NAME)) {
//								String rootPath = PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_ROOT_PATH);
//								rootPath=rootPath.replaceAll("/", "//");
//								String image2path=rootPath+fileList.get(0).getFilePath();
//								DataResultDTO rdto=checkVehicleRegistrationApiService.checkVehicleRegistration(image2path, d.getId(), CurrentThreadContext.getCurrentUserId());
//								BaseLogUtils.info(logger, "saveUpload", "抵押页校验---"+rdto);
//								if (rdto.getCode().equals("00000")) {
//									d.setCheckCount(1L);
//								}else {
//									d.setCheckCount(0L);
//								}
//							}
							this.arcApplyDetailService.modify(d);
						}else if(flag.equals("2")){//移交
							ArcTransferDetailVO vo=arcTransferDetailService.selectById(new Long(id));
							did=vo.getId();
							result.setId(did);
							vo.setEleId(did);
							this.arcTransferDetailService.modify(vo);
						}
						
						for (SysUlFilesRecVO sysUlFilesRecVO : fileList) {
							sysUlFilesRecVO.setPkId(String.valueOf(did));
							sysUlFilesRecVO.setTabNm(ArchiveConstants.FILE_ARC_MGR_DETAIL);
							sysUlFilesRecVO.setIsDel(BaseConstant.IS_YESNO_NO);
							sysUlFilesRecVO.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
							sysUlFilesRecVO.setSysCd(CurrentThreadContext.getCurrentSysCd());
							sysUlFilesRecVO.setCfgCd(ArchiveConstants.ARC005);
							sysUlFilesRecVO.setCfgDtlCd(ArchiveConstants.DOSSOER_TEMP_3);
							/*SysUlFilesRecDTO dto = SysUlFilesRecVOTransform.toDTO(sysUlFilesRecVO);
							dtoFileList.add(dto);*/
							sysUlFilesRecVO.setCrtTm(new Date());
							sysUlFilesRecVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
							Long vid=this.sysUlFilesRecService.add(sysUlFilesRecVO);
						}
						
					}else {
						result.setSuccess(BaseConstant.IS_YESNO_NO);
						result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
					}
				}
			}
		
		} catch (Exception e) {
			// TODO: handle exception
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(e.getMessage());
			BaseLogUtils.info(logger, "saveUpload", "失败.[id="+id+"] DATA:" +  e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	/**
	 * 更新状态
	 */
	@RequestMapping(value = "/updateMgrByApply.do", method = {RequestMethod.GET })
	@ResponseBody
	public DataResultVO updateMgrByApply(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String flag = request.getParameter("flag");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			if ("0".equals(flag)) {
				this.arcApplyFlowBusnessApiService.updateMgrByApply(Long.valueOf(id),false);
			}else if("1".equals(flag)){
				this.arcApplyFlowBusnessApiService.updateMgrByApply(Long.valueOf(id),true);
			}
			//this.arcApplyFlowBusnessApiService.noTransD(Long.valueOf(id));
			BaseLogUtils.info(logger, "updateMgrByApply", "更新状态.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "updateMgrByApply", "更新状态失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "updateMgrByApply", "更新状态失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}
	/**
	 *校验登记证书抵押页
	 */
	@RequestMapping(value = "/noTransD.do", method = {RequestMethod.GET })
	@ResponseBody
	public DataResultVO noTransD(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String flag = request.getParameter("flag");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {			
			this.arcApplyMainApiService.noTransD(Long.valueOf(id));
			BaseLogUtils.info(logger, "updateMgrByApply", "更新状态.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "updateMgrByApply", "更新状态失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "updateMgrByApply", "更新状态失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}
	/**
	 * 新流程到流程列表页面
	 */
	@RequestMapping(value = "/getFlowNew.do", method = { RequestMethod.GET})
	public String getFlowNew(HttpServletRequest request, HttpServletResponse response) {
		String flag = request.getParameter("flag");
		try {
			
			String id = request.getParameter("aid");
			
			request.setAttribute("flag",request.getParameter("flag"));
			//查找申请单
			ArcApplyMainVO avo= this.arcApplyMainService.selectById(Long.valueOf(id));
			//avo.setArcApplyDetailList(aList);
			String contractNo=avo.getContractNo();
			request.setAttribute("avo",avo);
			//查找筛选条件
			ArcMgrMainQC qc=new ArcMgrMainQC();
	//		if (StringUtils.isNotBlank(paySchId)) {
	//			qc.setPaySchId(Long.parseLong(paySchId));
	//		}
			qc.setContractNo(contractNo);
			Pager pager=new Pager();
			pager.setCurrentPage(0);
			pager.setEveryPage(10000);
			pager.setCondition(qc);
			List<ArcMgrMainVO> mgrList=(List<ArcMgrMainVO>)this.arcMgrMainService.selectListForListPage(pager).getResultList();			
			ArcMgrMainVO mgr=mgrList.get(0);
//			if (avo.getPortfolioNumber()!=null) {
//				mgr.setPortfolioNumber(avo.getPortfolioNumber());
//			}
//			if (avo.getPortfolioNumberProvince()!=null) {
//				mgr.setPortfolioNumberProvince(avo.getPortfolioNumberProvince());
//			}
			request.setAttribute("mgr",mgr);//筛选条件	
			//申请单明细
			ArcApplyDetailQC dQc=new ArcApplyDetailQC();
			dQc.setArcApplyMainId(Long.valueOf(id));
			List<ArcApplyDetailVO> aList=this.arcApplyDetailService.searchList(dQc);
			
			List<ArcApplyDetailVO> cList=new ArrayList<ArcApplyDetailVO>();//合同明细
			for (int i = 0; i < aList.size(); i++) {
				ArcApplyDetailVO vo=aList.get(i);
				if (vo.getContractNo()!=null&&vo.getPaylistCode()==null) {
					cList.add(vo);
				}
			}
			List<Map> pList=new ArrayList<Map>();//支付表明细
			
			for (int i = 0; i < mgrList.size(); i++) {
				ArcMgrMainVO vo=mgrList.get(i);
				Map map=new HashMap();
				map.put("paySchNo", vo.getPaySchNo());
				map.put("checkbz", "抵押登记证书真伪识别结果：无须上传");
				String checkbz="";
				List<ArcApplyDetailVO> ppList=new ArrayList<ArcApplyDetailVO>();//支付表明细
				for (int j = 0; j < aList.size(); j++) {
					ArcApplyDetailVO vo2=aList.get(j);
					if (vo2.getContractNo()!=null&&vo2.getPaylistCode()!=null&&vo.getPaySchNo().equals(vo2.getPaylistCode())) {
						ppList.add(vo2);
						
						//登记证书抵押页校验结果
						if (vo2.getFileName().contains(ArchiveConstants.VE_NAME)
								) {
							if (vo2.getBz()!=null) {
								checkbz +="抵押1结果："+vo2.getBz()+"。";
							}
							
							
						}
						if (vo2.getFileName().contains(ArchiveConstants.VE_NAME2)
								) {
							if (vo2.getBz()==null) {
								checkbz +="抵押2结果：没有附件。";
							}else {
								checkbz +="抵押2结果："+vo2.getBz()+"。";
							}
							
									
						}
					}
				}
				map.put("checkbz", checkbz);
				map.put("ppList", ppList);
				//添加租赁物信息
				//map.put("dtl", vo.getDtl());
				map.put("mvo", vo);
				if (ppList.size()>0) {
					pList.add(map);
				}
			}
			request.setAttribute("id",id);
			request.setAttribute("cList",cList);
			request.setAttribute("pList",pList);
			request.setAttribute("cjson",JSON.toJSON(cList));
			request.setAttribute("pjson",JSON.toJSON(pList));
			this.arcMgrMainService.setAttribute(request);//筛选条件查询
			//邮寄信息
			List<ArcPostInfoVO> plist=new ArrayList<ArcPostInfoVO>();
					
					ArcPostInfoQC condition2=new ArcPostInfoQC();
					condition2.setArcAppId(Long.valueOf(id));
					plist=this.arcPostInfoService.searchList(condition2);
			//初始化
//			if (plist!=null) {
//				for (int i = 0; i < plist.size(); i++) {
//					ArcPostInfoVO post=plist.get(i);
//					//客户经理
//					if ("003".equals(flag)&& "1".equals(post.getSendPorsonType())) {
//						request.setAttribute("post",post);
//						plist.remove(i);
//						break;
//					}
//					//商助
//					if ("001".equals(flag)&& "2".equals(post.getSendPorsonType())) {
//						request.setAttribute("post",post);
//						plist.remove(i);
//						break;
//					}
//				}
//			}
			
			request.setAttribute("plist",plist);
			BaseLogUtils.info(logger, "getFlow", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());

		} catch (Exception e) {
			// TODO: handle exception
			BaseLogUtils.error(logger, "getFlow", e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		if ("003".equals(flag)) {//客户经理重新提交
			return "archive/arcMgr/arcApplyFlowNew3";
		}else if ("001".equals(flag)) {//档案管理员
			return "archive/arcMgr/arcApplyFlowNew1";
		}else  {//上架if ("002".equals(flag))
			return "archive/arcMgr/arcApplyFlowNew2";
		}
	}
	/**
	 * 修改
	 */
	@RequestMapping(value = "/modifyFlowNew.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modifyFlowNew(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String flag = request.getParameter("flag");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modifyFlow", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		ArcApplyMainVO entity = null;
		try {
			entity = this.arcApplyMainService.selectById(Long.valueOf(id));
			//档案袋编号放入档案记录表
			ArcMgrMainQC mgrMainQC=new ArcMgrMainQC();
			mgrMainQC.setContractNo(entity.getContractNo());
			List<ArcMgrMainVO> mgrlist=this.arcMgrMainService.selectList(mgrMainQC);
			ArcMgrMainVO mgrMainVO=mgrlist.get(0);
			//档案袋编号生成
			//档案袋编号生成 上架时候更新
			if ("002".equals(flag)) {
				String code="";
				if ( StringUtils.isBlank(mgrMainVO.getPortfolioNumberProvince())) {//省
					code=this.arcApplyMainService.createPortfolioNumber(entity, "1");
					for (ArcMgrMainVO vo:mgrlist) {
						vo.setPortfolioNumberProvince(code);
						this.arcMgrMainService.modify(vo);
					}
				}else if( StringUtils.isBlank(mgrMainVO.getPortfolioNumber())) {//总部
					code=this.arcApplyMainService.createPortfolioNumber(entity, "2");
					for (ArcMgrMainVO vo:mgrlist) {
						vo.setPortfolioNumber(code);
						this.arcMgrMainService.modify(vo);
					}
				}
			}
			//if ("001".equals(flag)||"003".equals(flag))  {
			entity.setMdfUsrId(new Long(flag));
			this.arcApplyMainService.modify(entity);
			//}
			//更新电子合同
			String contractNo=entity.getContractNo();
			ArcMgrMainVO cVo=null;
			ArcMgrMainQC qc=new ArcMgrMainQC();
			qc.setContractNo(contractNo);
			String sysCd = CurrentThreadContext.getCurrentSysCd();
			String orgCd = CurrentThreadContext.getCurrentOrgCd();
			//已经归档的支付表
			List<ArcMgrDetailVO> ddlist=new ArrayList<ArcMgrDetailVO>();
			List<ArcMgrMainVO> mgrList=(List<ArcMgrMainVO>)this.arcMgrMainService.selectList(qc);
			for (int i = 0; i < mgrList.size(); i++) {
				ArcMgrMainVO arcMgrMainVO =mgrList.get(i);
				ArcMgrDetailQC dQc = new ArcMgrDetailQC();
				dQc.setArcMgrMainId(arcMgrMainVO.getId());
				List<ArcMgrDetailVO> list = ArcMgrDetailVOTransform
						.toVOList(this.arcMgrDetailApiService.searchList(dQc, sysCd, orgCd));
				arcMgrMainVO.setArcMgrDetailList(list);
				if (arcMgrMainVO.getPaySchId()==null) {
					cVo=arcMgrMainVO;
				}
				ddlist.addAll(list);
			}			 
			this.arcMgrMainService.setFilSign(mgrList, cVo);			
			
			//申请单明细
			ArcApplyDetailQC dQc=new ArcApplyDetailQC();
			dQc.setArcApplyMainId(Long.valueOf(id));
			List<ArcApplyDetailVO> cList=this.arcApplyDetailService.searchList(dQc);
			Map map=this.getParms(request);
			
			//entity.setName(name);
			if (cList==null) {
				cList=new ArrayList<ArcApplyDetailVO>();
			}
			//更新合同明细 支付表明细
			for (int i = 0; i < cList.size(); i++) {
				ArcApplyDetailVO vo=cList.get(i);
				if ("001".equals(flag)) {
					//实际归档份数
					if (map.get("checkCount_p_"+vo.getId())!=null&&!"".equals(map.get("checkCount_p_"+vo.getId()))) {
						vo.setThisCount(Long.valueOf(map.get("checkCount_p_"+vo.getId()).toString().replaceAll(" ", "")));
						vo.setDossierCountI(vo.getThisCount());
						long sj=vo.getThisCount().longValue()+(vo.getCheckCount()==null?0L:vo.getCheckCount().longValue());
						vo.setCheckCount(sj);
						vo.setThisCount(0L);
					}else {
						//vo.setCheckCount(0L);
					}
					//要求归档份数
					if (map.get("checkCount_d_"+vo.getId())!=null&&!"".equals(map.get("checkCount_d_"+vo.getId()))) {
						vo.setDossierCount(Long.valueOf(map.get("checkCount_d_"+vo.getId()).toString().replaceAll(" ", "")));
					}
				}else if ("002".equals(flag)) {
					//更新上架数
					if (map.get("thisCount_p_"+vo.getId())!=null&&!"".equals(map.get("thisCount_p_"+vo.getId()))) {
						vo.setThisCountSj(Long.valueOf(map.get("thisCount_p_"+vo.getId()).toString().replaceAll(" ", "")));
						long sj=vo.getThisCountSj().longValue()+(vo.getCheckCountSj()==null?0L:vo.getCheckCountSj().longValue());
						vo.setCheckCountSj(sj);
						vo.setThisCountSj(0L);
					}else {
						vo.setThisCountSj(0L);
					}
				}else if ("003".equals(flag)) {
					if (map.get("thisCount_p_"+vo.getId())!=null&&!"".equals(map.get("thisCount_p_"+vo.getId()))) {
						vo.setThisCount(Long.valueOf(map.get("thisCount_p_"+vo.getId()).toString().replaceAll(" ", "")));
					}else {
						vo.setThisCount(0L);
					}
				}
				//有外联附件的更新eleid
				if (map.get("ele_"+vo.getId())!=null&&!"".equals(map.get("ele_"+vo.getId()))) {
					vo.setEleId(Long.valueOf(map.get("ele_"+vo.getId()).toString().replaceAll(" ", "")));
				}
				
				//更新电子合同
				if (vo.getDossierTemp().equals("3")) {
					for (ArcMgrDetailVO dvo: ddlist) {
											
						if (dvo.getArcReceiptDetailId().equals(vo.getArcReceiptDetailId())
								&&(dvo.getPaylistCode()==null||dvo.getPaylistCode().equals(vo.getPaylistCode()))
								&&dvo.getEleId()!=null) {
							vo.setEleId(dvo.getEleId());
							vo.setCheckCount(1L);
						}
					}
				}				
				this.arcApplyDetailService.modify(vo);
			}
			//保存邮寄信息
			ArcPostInfoVO pVo=new ArcPostInfoVO();
			pVo.setArcAppId(Long.valueOf(id));
			//保存邮寄信息
			map.put("arcAppId", id);
			map.put("arcAppType", "1");
			map.put("arcAppBussType", "0");
			//商助
			
			if ("001".equals(flag)||"003".equals(flag)) {
				map.put("sendPorsonType", "2");
				this.arcPostInfoService.addOrUpdate(map);
			}
			
//			this.arcApplyFlowBusnessApiService.updateMgrByApply(Long.valueOf(id), true);
//			boolean success = this.arcApplyMainService.modify(entity);
//			if(!success){
//				result.setSuccess(BaseConstant.IS_YESNO_NO);
//				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
//				BaseLogUtils.info(logger, "modifyFlow", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
//			}
			BaseLogUtils.info(logger, "modifyFlow", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modifyFlow", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modifyFlow", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	/**
	 *REQ-12833 线上换车 	 arcInfoFlag=1 权证归档节点	arcInfoFlag=2 合同管理员补充节点
	  	保存换车补充协议
	 */
	@RequestMapping(value = "/modifyMgrDetail.do")
	@ResponseBody
	public Object modifyMgrDetail(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String arcInfoFlag = request.getParameter("arcInfoFlag");//标识
		String contractNo = request.getParameter("contractNo");//合同号
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		BaseLogUtils.info(logger, "modifyMgrDetail", ".[contractNo="+contractNo+"]"+"arcInfoFlag"+arcInfoFlag, CurrentThreadContext.getCurrentUserName());
		if(StringUtils.isEmpty(contractNo)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		Map map=this.getParms(request);
		Long arcDetailId = null ;//换车补充协议档案管理明细表id
		Long arcReceiptDetailId = null ;//换车补充协议交接单明细表id
		try {
			ArcApplyDetailVO  applyDetail = new ArcApplyDetailVO(); //申请明细表
			ArcMgrDetailVO mgrDetail = new ArcMgrDetailVO();//档案管理明细表
			//拼一下本次归档份数
			Long dossierCountI =0l;
			if(map.containsKey("arcDetailId")){
				arcDetailId =Long.parseLong(map.get("arcDetailId").toString());
			}
			if(map.containsKey("arcReceiptDetailId")) {
				arcReceiptDetailId=Long.parseLong(map.get("arcReceiptDetailId").toString());
				String temp = "thisCount_c_code_"+arcReceiptDetailId;
				if(map.containsKey(temp)){//thisCount_c_code_200813322001550: "7"
					dossierCountI = Long.parseLong(map.get("thisCount_c_code_"+arcReceiptDetailId).toString());
				}
			}
			//根据申请明细id获取VO
			applyDetail = arcApplyDetailService.selectById(arcDetailId);
			BaseLogUtils.newServiceLogger("==applyDetail=="+JSONObject.toJSONString(applyDetail)).info();
			if(applyDetail==null){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("申请表明细表数据异常");
				return result;
			}
			//根据合同号和交接单明细id获取换车补充协议  档案管理明细表
			ArcMgrDetailQC qc = new ArcMgrDetailQC();
			qc.setContractNo(contractNo);
			qc.setArcReceiptDetailId(arcReceiptDetailId);
			List<ArcMgrDetailVO> mgrDetailList = arcMgrDetailService.searchList(qc);
			BaseLogUtils.newServiceLogger("==mgrDetailList=="+JSONObject.toJSONString(mgrDetailList)).info();
			if(CollectionUtils.isNotEmpty(mgrDetailList)){
				mgrDetail = mgrDetailList.get(0);
			}else {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("档案管理明细表数据异常");
				return result;
			}
			if("2".equals(arcInfoFlag)){ //合同管理员补充节点
				//更新 申请明细表
				applyDetail.setId(arcDetailId);
				applyDetail.setThisCount(dossierCountI);//本次归档份数 
				BaseLogUtils.newServiceLogger("modifyMgrDetail 合同管理1"+JSONObject.toJSONString(applyDetail)).info();
				this.arcApplyDetailService.modify(applyDetail);
				//更新档案管理明细表
				ArcMgrDetailVO mgrDetailVO = new ArcMgrDetailVO();
				mgrDetailVO.setId(mgrDetail.getId());
				mgrDetailVO.setDossierCountI(dossierCountI);//本次归档份数 //ArcMgrDetailVO T实际归档份数
				BaseLogUtils.newServiceLogger("modifyMgrDetail 合同管理2"+JSONObject.toJSONString(mgrDetailVO)).info();
				this.arcMgrDetailService.modify(mgrDetailVO);
				
			}
			if("1".equals(arcInfoFlag)){ //权证归档节点
				//更新档案管理明细表
				mgrDetail.setId(mgrDetail.getId());
				mgrDetail.setDossierCountI(dossierCountI);//本次归档份数 //ArcMgrDetailVO T实际归档份数
				BaseLogUtils.newServiceLogger("modifyMgrDetail 合同管理2"+JSONObject.toJSONString(mgrDetail)).info();
				this.arcMgrDetailService.modify(mgrDetail);
				//更新申请明细表
				//上架 THIS_COUNT_SJ	NUMBER	Y			本次上架份数 CHECK_COUNT_SJ	NUMBER	Y	0		实际上架份数
				//deVO thisCountSj checkCountSj
				applyDetail.setThisCountSj(dossierCountI);
				applyDetail.setCheckCountSj(dossierCountI);
				applyDetail.setThisCount(dossierCountI);//本次归档份数
				BaseLogUtils.newServiceLogger("modifyMgrDetail 权证归档"+JSONObject.toJSONString(applyDetail)).info();
				arcApplyDetailService.modify(applyDetail);
				
			}
			//走一下校验接口逻辑
			//arcMgrMainService.removeMgrInforByContractNo(contractNo,2);
			
			BaseLogUtils.info(logger, "modifyMgrDetail", "修改成功.[contractNo="+contractNo+"] DATA:" + JSONObject.toJSONString(map), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modifyMgrDetail", "修改失败.[contractNo="+contractNo+"] DATA:" + JSONObject.toJSONString(map) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modifyMgrDetail", "修改失败.[contractNo="+contractNo+"] DATA:" + JSONObject.toJSONString(map) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
}

